package org.swim.servlet;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.entity.AddAbilityRequest;
import org.swim.entity.User;
import org.swim.session.AbilityRequestManagerRemote;

/**
 * Servlet implementation class ViewAbilityRequests
 */
public class ViewAbilityRequests extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewAbilityRequests() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        InitialContext ic;
        if (user.isAdmin()==true) {
            try {
      			ic = new InitialContext();
      			AbilityRequestManagerRemote abilityRequestManager = (AbilityRequestManagerRemote)ic.lookup("AbilityRequestManagerRemote");
      			List<AddAbilityRequest> abilitiesRequests = abilityRequestManager.getAllSuspendedAbilityRequest();
      			request.setAttribute("abilitiesRequests", abilitiesRequests);
      			request.setAttribute("areThereAbilitiesRequests", true);
      		} catch (NamingException e) {
      			e.printStackTrace();
      		} catch (NullPointerException e) {
      			request.setAttribute("areThereAbilitiesRequests", false);
      		}       	
        } else {
        	request.setAttribute("areThereAbilitiesRequests", false);
        }

		request.getRequestDispatcher("WEB-INF/ViewAbilityRequests.jsp").forward(request, response);	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
